package com.link.base.base.invloc.service;

import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.invloc.dao.mybatis.mapper.InvlocMapper;
import com.link.base.base.invloc.model.Invloc;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author: unknown
 * created: 2018/5/8
 * comments:
 */
@Service
@Transactional(rollbackFor = BasicServiceException.class)
public class InvlocServiceImpl extends BasicServiceImpl<Invloc> implements InvlocService {

    @Resource
    private InvlocMapper invlocMapper;
    @Resource
    private InvlocService invlocService;
    @Resource
    private AccountService accountService;

    @Override
    public BasicMapper<Invloc> getBasicMapper() {
        return invlocMapper;
    }

    @Override
    public List<Invloc> queryByAcctId(Invloc param) throws Exception {
        return invlocMapper.queryByAcctId(param);
    }

    /**
     * <p>根据客户信息添加仓库</p>
     *
     * @param account
     * @author guoshan
     */
    @Override
    public Invloc insertByAccount(Account account) throws Exception {
        Invloc invloc = new Invloc();
        invloc.setOwnerId(account.getId());
        invloc.setInvlocName(account.getAcctName());
        Long invlocId = keyGenerateService.keyGenerate();
        invloc.setId(invlocId);
        invloc.setInvlocType("");
        invloc.setOrgId(account.getOrgId());
        invloc.setOrgName(account.getOrgName());
        invloc.setOwnerName(account.getAcctName());
       // invloc.setInvoiceAddr(account.getAddrDetailAddr());
        invloc.setShipmentAddr("");
        invloc.setReciveAddr(account.getPrivateAddr());
        insert(invloc);
        return queryById(invlocId);
    }

    @Override
    public Invloc getFictitiousInvloc() throws Exception {
        //虚拟仓库是否存在，存在则用虚拟仓库，反之创建虚拟仓库
        Invloc invlocParam = new Invloc();
        invlocParam.setInvlocType("FICTITIOUS");
        invlocParam.setPageFlag(false);
        //查询虚拟仓库排序标识
        invlocParam.setAttr1("true");
        List<Invloc> invlocList = invlocService.queryByExamplePage(invlocParam);
        Invloc invloc = null;
        if (invlocList.isEmpty()) {
            //创建一个虚拟仓库
            Invloc virtualInvloc = new Invloc();
            //virtualInvloc.setOwnerId("VIRTUAL");
            //virtualInvloc.setOrgId("VIRTUAL");
            virtualInvloc.setOwnerId(0L);
            virtualInvloc.setOrgId(0L);
            virtualInvloc.setInvlocType("FICTITIOUS");
            virtualInvloc.setInvlocName("虚拟仓库");
            invlocService.insert(virtualInvloc);
            invloc = invlocService.queryById(virtualInvloc.getId());
        } else {
            invloc = invlocList.get(0);
        }
        return invloc;
    }

    @Override
    public Invloc getAccountInvloc(Long accountId) throws Exception {
        if (null == accountId) {
            throw new ServiceException("PUBLIC-026");
        }
        Account account = accountService.queryById(accountId);
        Invloc invloc = getAccountInvloc(account);
        return invloc;
    }

    @Override
    public Invloc getAccountInvloc(Account account) throws Exception {
        Invloc invlocParam = new Invloc();
        invlocParam.setOwnerId(account.getId());
        List<Invloc> invlocList = invlocService.queryByAcctId(invlocParam);
        Invloc invloc = null;
        if (invlocList.isEmpty()) {
            //根据客户创建一个仓库
            invloc = insertByAccount(account);
        } else {
            invloc = invlocList.get(0);
        }
        return invloc;
    }

    /**
     * <p>获取客户仓库</p>
     *
     * @param account
     * @author guoshan
     */
    @Override
    public Invloc getInvloc(Account account) throws Exception {
        String accountType = account.getAcctType();
        Invloc invloc = null;
        String vendor = "Vendor";
        String dealer = "Dealer";
        String terminal = "Terminal";
        if (vendor.equals(accountType) || dealer.equals(accountType) || terminal.equals(accountType)) {
            invloc = getAccountInvloc(account);
        } else {
            invloc = getFictitiousInvloc();
        }
        return invloc;
    }
}
